public class StringStateMachine {

    // 定义状态枚举
    enum State {
        PARSING("解析状态"),
        ESCAPE_CHARACTER("转义字符状态"),
        ADDING_CHARACTERS("添加字符状态"),
        IN_DOUBLE_QUOTES("遇到双引号");

        private final String description;

        State(String description) {
            this.description = description;
        }

        @Override
        public String toString() {
            return description;
        }
    }

    // 当前状态
    private State currentState = State.PARSING;
    // 存储解析结果的字符串
    private StringBuilder result = new StringBuilder();
    // 标记是否处于转义状态
    private boolean isEscaped = false;
    // 标记是否处于双引号内
    private boolean inDoubleQuotes = false;

    // 处理单个字符
    public void processCharacter(char c) {
        switch (currentState) {
            case PARSING:
                if (c == '"') {
                    currentState = State.IN_DOUBLE_QUOTES;
                    inDoubleQuotes = true;
                } else if (c == '\\') {
                    currentState = State.ESCAPE_CHARACTER;
                    isEscaped = true;
                } else {
                    currentState = State.ADDING_CHARACTERS;
                    addToResult(c);
                }
                break;
            case ESCAPE_CHARACTER:
                // 转义字符后，无论遇到什么字符都添加到字符串中
                addToResult(c);
                currentState = inDoubleQuotes ? State.ADDING_CHARACTERS : State.PARSING;
                isEscaped = false;
                break;
            case ADDING_CHARACTERS:
                if (inDoubleQuotes && c == '"') {
                    currentState = State.PARSING;
                    inDoubleQuotes = false;
                } else {
                    addToResult(c);
                }
                break;
            case IN_DOUBLE_QUOTES:
                // 在双引号内，遇到反斜杠则进入转义字符状态，否则继续添加字符
                if (c == '\\') {
                    currentState = State.ESCAPE_CHARACTER;
                    isEscaped = true;
                } else {
                    addToResult(c);
                }
                break;
        }
    }

    // 将字符添加到结果字符串中
    private void addToResult(char c) {
        if (!isEscaped) {
            // 如果不是转义字符，则按原样添加
            result.append(c);
        } else {
            // 如果是转义字符，这里可以根据需要进行特殊处理，例如将'\n'转换为换行符等
            // 但由于题目未给出具体转义规则，这里简单地将转义字符和其后的字符一起添加
            result.append('\\').append(c);
            isEscaped = false; // 重置转义状态
        }
    }

    // 获取解析后的字符串
    public String getResult() {
        return result.toString();
    }

    // 测试方法
    public static void main(String[] args) {
        StringStateMachine machine = new StringStateMachine();
        String testString = "This is a \"test\\nstring\" with escaped characters.";
        for (char c : testString.toCharArray()) {
            machine.processCharacter(c);
        }
        System.out.println(machine.getResult());
    }
}